<?php
class ControllerCatalogAttributeGroup extends AdminController {

  	public function index() {		
		M('catalog/attribute_group');

    	$this->getList();
  	}

  	public function insert() {		
		M('catalog/attribute_group');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_catalog_attribute_group->addAttributeGroup($this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);		
      		$this->redirect(UA('catalog/attribute_group'));
		}
    	$this->getForm();
  	}

  	public function update() {
		M('catalog/attribute_group');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_catalog_attribute_group->editAttributeGroup($this->request->get['attribute_group_id'], $this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/attribute_group'));
    	}
    	$this->getForm();
  	}

  	public function delete() {
		M('catalog/attribute_group');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $attribute_group_id) {
				$this->model_catalog_attribute_group->deleteAttributeGroup($attribute_group_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/attribute_group'));
   		}
    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$attribute_group_total = $this->model_catalog_attribute_group->getTotalAttributeGroups();

		$qf = new Query_filter();
		$this->data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups($qf);

		//page
		$this->data['pagination'] = $this->pagination($attribute_group_total)->render();

		$this->display('catalog/attribute_group_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$attribute_group_id = G('attribute_group_id');
		
		if ($attribute_group_id) {
			$this->data['action'] = UA('catalog/attribute_group/update', 'attribute_group_id=' . $attribute_group_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_catalog_attribute_group->getAttributeGroup($attribute_group_id);
				$this->vo->data['attribute_group_description'] = 
					$this->model_catalog_attribute_group->getAttributeGroupDescriptions($attribute_group_id);
			}			
		} else {
			$this->data['action'] = UA('catalog/attribute_group/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		$this->display('catalog/attribute_group_form.tpl');
  	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
    	foreach ($this->request->post['attribute_group_description'] as $language_id => $value) {
      		$this->form_validation->set_rules("attribute_group_description[$language_id][name]", '', 'required|range_length[2,64]', $this->_['error_name']);
    	}

		return $this->form_validation->run();
  	}

  	protected function validateDelete() {
		M('catalog/attribute');

		foreach ($this->request->post['selected'] as $attribute_group_id) {
			$attribute_total = $this->model_catalog_attribute->getTotalAttributesByAttributeGroupId($attribute_group_id);

			if ($attribute_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_attribute'], $attribute_total));
				return false;
			}
	  	}

		return true;
  	}
	
	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			M('catalog/attribute_group');
			$json = array();

			$results = $this->model_catalog_attribute_group->getAttributeGroups(new Query_filter(null, false));
			
			foreach ($results as $result) {
				$json[] = array(
					'attribute_group_id'    => $result['attribute_group_id'],
					'name'            => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>